﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Base;
using BBMS.DLL.DataContract;
using System.Collections.Specialized;
using System.Collections;

namespace BBMS.DLL.Forms
{
    public partial class frmUserCounter : Base.FrmEditBase
    {
        private List<Quay> dsQuayList = new List<Quay>();
        private List<Quay> quayList = new List<Quay>();
        private List<Quay> dsQuayNVList = new List<Quay>();
        private int NVID;
        private NhanVienQuay nvq = new NhanVienQuay();
        public frmUserCounter()
        {
            InitializeComponent();
           // GetQuayList();
            GetUserList();

        }

        private void GetUserList()
        {
            System.Collections.ArrayList ar = NhanVienList.getAllUserName();
            for (int i = 0; i < ar.Count; i++)
            {
                KeyValuePair kvp = (KeyValuePair)ar[i];
                cbxUsername.Items.Add(kvp);
            }
            
            cbxUsername.SelectedIndexChanged += new EventHandler(cbxUsername_SelectedIndexChanged);
            cbxUsername.SelectedIndex = 0;
        }

        private void ClearUserInfo()
        {
            txtFullName.Text = "";
            txtMobile.Text = "";
            txtDepartment.Text = "";
            txtPosition.Text = "";
        }

        private void GetQuayList()
        {
            ArrayList arret = QuayList.getAllMaQuay();
            // tvMSQuayPhuTrach.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    TreeNode tn = new TreeNode();
                    tn.Text = kvp.Value.ToString();
                    tn.Name = kvp.Key.ToString();
                    tvMSQuayPhuTrach.Nodes.Add(tn);
                   
                    // tvMSQuayChuaPhuTrach.Nodes.Add(tn);
                    //tvMSQuayPhuTrach.Nodes.Add(kvp.Key.ToString(), kvp.Value.ToString());
                    // tvMSQuayChuaPhuTrach.Nodes.Add(kvp.Key.ToString(), kvp.Value.ToString());
                }
            }
            //cbxMaNCC.SelectedIndex = 0;
        }

        private void cbxUsername_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetNhanVienQuay();
        }

        private void GetNhanVienQuay()
        {
            dsQuayList = new List<Quay>();
            dsQuayNVList = new List<Quay>();
            quayList = new List<Quay>();
            while (tvMSQuayChuaPhuTrach.Nodes.Count > 0) tvMSQuayChuaPhuTrach.Nodes.RemoveAt(0);
            while (tvMSQuayPhuTrach.Nodes.Count > 0) tvMSQuayPhuTrach.Nodes.RemoveAt(0);
            KeyValuePair kvp = (KeyValuePair)cbxUsername.SelectedItem;
            ClearUserInfo();
            NhanVien nv = (NhanVien)kvp.Key;
            txtFullName.Text = nv.FullName;
            txtMobile.Text = nv.Mobile;
            txtDepartment.Text = nv.Department;
            txtPosition.Text = nv.Position;
            nvq = new NhanVienQuay(nv.ID);
            NVID = nv.ID;
            dsQuayNVList = nvq.MaQuay;
            ArrayList arret = QuayList.getAllMaQuay();
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    
                    KeyValuePair kvp1 = (KeyValuePair)arret[i];
                    Quay q = new Quay(kvp1.Key.ToString(), kvp1.Value.ToString(), "", "");
                    bool exists = false;
                    TreeNode tn = new TreeNode();
                    tn.Name = q.Ma;
                    tn.Text = q.Ten;
                    quayList.Add(q);
                    for (int j = 0; j < dsQuayNVList.Count; j++)
                    {
                        if (dsQuayNVList[j].Ma == q.Ma)
                        {
                            tvMSQuayPhuTrach.Nodes.Add(tn);
                            exists = true;
                            break;
                        }
                    }
                    if (!exists)
                    {
                        tvMSQuayChuaPhuTrach.Nodes.Add(tn);
                        dsQuayList.Add(q);
                    }
                }
            }
        }
      

        private void frmUserCounter_Load(object sender, EventArgs e)
        {
            cbBranch.Items.Clear();
            NameValueCollection branchCollection = NghanhHangList.getAllBranch();
            AutoCompleteStringCollection autoCompleteSrc = new AutoCompleteStringCollection();

            foreach (string key in branchCollection.AllKeys)
            {
                cbBranch.Items.Add(key + " - " + branchCollection.Get(key));
                autoCompleteSrc.Add(key + " - " + branchCollection.Get(key));
            }
            cbBranch.AutoCompleteCustomSource = autoCompleteSrc;
            if (cbBranch.Items.Count > 0) cbBranch.SelectedIndex = 0;
        }

        private void btnAddOne_Click(object sender, EventArgs e)
        {
            
            if (tvMSQuayPhuTrach.SelectedNode != null)
            {
                List<TreeNode> tnList = new List<TreeNode>();         
                foreach (TreeNode tn in tvMSQuayPhuTrach.SelectedNodes)
                {
                    tnList.Add(tn);      
                    for (int j=0;j<dsQuayNVList.Count;j++)
                        if (dsQuayNVList[j].Ma == tn.Name)
                        {
                            dsQuayNVList.RemoveAt(j);
                            break;
                        }
                    dsQuayList.Add(new Quay(tn.Name,tn.Text,"",""));
                }       
                for (int i = 0; i < tnList.Count; i++)
                {
                    
                    tvMSQuayPhuTrach.Nodes.Remove(tnList[i]);
                    tvMSQuayChuaPhuTrach.Nodes.Add(tnList[i]);
                }

            }
            else
            {
                MessageBox.Show(null, "Bạn phải chọn quầy trên danh sách quầy", "Thông báo");
            }
        }

        private void btnRemoveOne_Click(object sender, EventArgs e)
        {
            if (tvMSQuayChuaPhuTrach.SelectedNode != null)
            {
                List<TreeNode> tnList = new List<TreeNode>();
                foreach (TreeNode tn in tvMSQuayChuaPhuTrach.SelectedNodes)
                {
                    tnList.Add(tn);
                    for (int j = 0; j < dsQuayList.Count; j++)
                        if (dsQuayList[j].Ma == tn.Name)
                        {
                            dsQuayList.RemoveAt(j);
                            break;
                        }
                    dsQuayNVList.Add(new Quay(tn.Name, tn.Text, "", ""));
                }
                for (int i = 0; i < tnList.Count; i++)
                {
                    tvMSQuayChuaPhuTrach.Nodes.Remove(tnList[i]);
                    tvMSQuayPhuTrach.Nodes.Add(tnList[i]);
                }
            }
            else
            {
                MessageBox.Show(null, "Bạn phải chọn quầy trên danh sách quầy", "Thông báo");
            }
        }

        private void btnAddAll_Click(object sender, EventArgs e)
        {
          // for (int i = 0; i < tvMSQuayPhuTrach.Nodes.Count; i++)
            while (tvMSQuayPhuTrach.Nodes.Count > 0)
            {
                TreeNode tn = tvMSQuayPhuTrach.Nodes[0];
                tvMSQuayPhuTrach.Nodes.Remove(tn);
                tvMSQuayChuaPhuTrach.Nodes.Add(tn);
            }
            dsQuayNVList = new List<Quay>();
            dsQuayList = quayList;
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
//            for (int i = 0; i < tvMSQuayChuaPhuTrach.Nodes.Count; i++)
            while (tvMSQuayChuaPhuTrach.Nodes.Count > 0)
            {
                TreeNode tn = tvMSQuayChuaPhuTrach.Nodes[0];
                tvMSQuayChuaPhuTrach.Nodes.Remove(tn);
                tvMSQuayPhuTrach.Nodes.Add(tn);
            }
            dsQuayList = new List<Quay>();
            dsQuayNVList = quayList;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            GetNhanVienQuay();
        }

        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            OnSaveAndNew();
        }

        private void btnSaveClose_Click(object sender, EventArgs e)
        {
            OnSaveAndClose();
        }

        protected override ObjectBase GetEditedObject()
        {
            return new NhanVienQuay(NVID, dsQuayNVList);
        }

        protected override void SaveObject(bool close)
        {
            ObjectBase obj = GetEditedObject();
            if (obj != null)
            {
                String msg = "";             
                msg = obj.InsertNew();
                if (msg.Length == 0)
                {
                    MessageBox.Show(null, "Đã cập nhật quầy cho nhân viên thành công!", "Thông báo");
                    if (close)
                    { // Luu & Dong ?
                        this.Close();
                    }
                    else
                    { // Luu va them moi
                       
                       // Reset();
                    }
                }
                else MessageBox.Show(msg);
            }
        }
    }
}
